package cc.rengu.igas.mcps.core.service.trans.stagepay;

import cc.rengu.igas.channel.cups.facade.response.ConsumeResponse;
import cc.rengu.igas.channel.upqc.facade.result.Result;
import cc.rengu.igas.mcps.common.constant.McpsParamConstant;
import cc.rengu.igas.mcps.common.constant.McpsTreeNodeConstant;
import cc.rengu.igas.mcps.common.enums.AcctTypeEnum;
import cc.rengu.igas.mcps.common.enums.McpsAcctTypeEnum;
import cc.rengu.igas.mcps.common.enums.RespCodeEnum;
import cc.rengu.igas.mcps.core.model.AcctAndFeeType;
import cc.rengu.igas.mcps.core.realize.PosParamDealService;
import cc.rengu.igas.mcps.core.realize.impl.PosParamDealServiceImpl;
import cc.rengu.igas.mcps.core.service.base.CancelService;
import cc.rengu.igas.mcps.facade.request.InstallmentCancelRequest;
import cc.rengu.igas.mcps.facade.response.InstallmentCancelResponse;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.igas.share.common.util.GlobalEsbUtil;
import cc.rengu.igas.share.core.model.MchntInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.*;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 分期撤销
 * Created by zhangbin on 2020/4/3
 */
public class InstallmentCancelService extends CancelService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        InstallmentCancelRequest installmentCancelRequest = new InstallmentCancelRequest();
        ConvertUtil.convertOutput(installmentCancelRequest);
        return installmentCancelRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        InstallmentCancelRequest installmentCancelRequest = (InstallmentCancelRequest) request;
        //重组报文
        InstallmentCancelResponse installmentCancelResponse = new InstallmentCancelResponse();
        installmentCancelResponse.setHeader(installmentCancelRequest.getHeader());
        BizResponse bizResponse = new BizResponse();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String sysNo = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        /*获取账户类型与费率类型*/
        AcctAndFeeType acctAndFeeType = (AcctAndFeeType) xmlTreeUtil.getXmlTreeObjectValue(McpsTreeNodeConstant.ACCT_AND_FEE_TYPE, AcctAndFeeType.class);
        /* TODO 只允许本行贷记卡分期消费撤销 */
        if (!McpsAcctTypeEnum.OWN_CREDIT.getAcctType().equals(acctAndFeeType.getAcctType())) {
            rglog.error("只允许本行贷记卡分期消费撤销!! acctType:{}", acctAndFeeType.getAcctType());
            throw new BizException(RespCodeEnum.ACCT_TYPE_NOT_SUPPORT.getRespCode(), RespCodeEnum.ACCT_TYPE_NOT_SUPPORT.getRespDesc());
        }

        OrderService orderService = new OrderServiceImpl();
        //获取原用户订单
        UserOrderInfo origUserOrderInfo = (UserOrderInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.ORIG_USER_ORDER_INFO);
        //获取原商户订单
        MchntOrderInfo origMchntOrderInfo = (MchntOrderInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.ORIG_MCHNT_ORDER_INFO);
        //初始化用户订单表
        UserOrderInfo userOrderInfoCancel = initOrderInfoUser(installmentCancelRequest, origUserOrderInfo);
        //初始化商户订单表
        MchntOrderInfo mchntOrderInfoCancel = initOrderInfoMchnt(installmentCancelRequest, userOrderInfoCancel, origMchntOrderInfo);
        /*注册登计订单*/
        List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
        mchntOrderInfoList.add(mchntOrderInfoCancel);

        //获取原esb通道支付订单
        PayOrderInfo oriPayOrderInfoESB = (PayOrderInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.ORIG_PAY_ORDER_INFO);
        /*创建esb通道支付订单*/
        PayOrderInfo payOrderInfoCancel = initPayOrderInfo(oriPayOrderInfoESB, installmentCancelRequest, userOrderInfoCancel);
        if (!orderService.registerOrderInfo(userOrderInfoCancel, mchntOrderInfoList, payOrderInfoCancel)) {
            rglog.error("登记订单失败!");
            throw new BizException(RespCodeEnum.POS_RSP_TIME_OUT.getRespCode(), RespCodeEnum.POS_RSP_TIME_OUT.getRespDesc());
        }

        //转pin
        String newPin = installmentCancelRequest.getPin();
        /*判断特殊费率商户*/
        String feeInputModeFlag = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SPECIAL_FEE_INPUT_MODE);

        //调用esb分期撤销接口
        if (McpsAcctTypeEnum.OWN_CREDIT.getAcctType().equals(acctAndFeeType.getAcctType())) {
            updcCancel(installmentCancelRequest, origUserOrderInfo, origMchntOrderInfo, oriPayOrderInfoESB,
                    newPin, mchntOrderInfoCancel, userOrderInfoCancel, payOrderInfoCancel, feeInputModeFlag, installmentCancelResponse);
        }
        /*特殊费率商户交易成功后调用微信进行消息推送*/
        if ("Y".equals(feeInputModeFlag) && TransStatusEnum.TRANS_SUCC.getStatus().equals(payOrderInfoCancel.getTransStatus()) && BizStatusEnum.FINISH.getStatus().equals(payOrderInfoCancel.getBizStatus())) {
            GlobalTransUtil.setCacheValue(TransCacheConstant.USER_ORDER_INFO, userOrderInfoCancel);
            int iReturnCode = asynnotify.transactionNotice("MchntTransNoticeService", 1, 0, 0);
            if (0 != iReturnCode) {
                rglog.error("提交交易结果异步通知给商户失败!");
            }
        }

        installmentCancelResponse.setPayId(sysNo);
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(payOrderInfoCancel.getRespCode());
        bizResponse.setRespDesc(payOrderInfoCancel.getRespDesc());
        bizResponse.setResult(installmentCancelResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        InstallmentCancelResponse installmentCancelResponse = (InstallmentCancelResponse) bizResponse.getResult();
        installmentCancelResponse.setRespCode(bizResponse.getRespCode());
        installmentCancelResponse.setRespDesc(bizResponse.getRespDesc());
        ConvertUtil.convertInput(installmentCancelResponse);
    }

    /**
     * 登记支付订单表
     *
     * @param installmentCancelRequest 请求信息
     * @throws Exception 异常
     */
    private PayOrderInfo initPayOrderInfo(PayOrderInfo origPayOrderInfo, InstallmentCancelRequest installmentCancelRequest, UserOrderInfo userOrderInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        PosParamDealService posParamDealService = new PosParamDealServiceImpl();
        /* 登记支付订单信息 */
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String txnNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TXN_NUM);
        String bizType = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.BIZ_TYPE);
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setTxnDate(DateUtil.getCurrentDate());
        payOrderInfo.setTxnTime(DateUtil.getCurrentTime());
        payOrderInfo.setTxnNum(txnNum);
        payOrderInfo.setBizType(bizType);
        payOrderInfo.setSysSeqNum(sysSeqNum);
        payOrderInfo.setMchntNo(installmentCancelRequest.getMchntNo());
        payOrderInfo.setMchntOrderId(userOrderInfo.getMchntOrderId());
        payOrderInfo.setOrderCcy(AppParamConstant.DEFAULT_ORDER_CCY);
        payOrderInfo.setOrderAmt(origPayOrderInfo.getOrderAmt());
        payOrderInfo.setMsgDstId(ChannelEnum.CUPS.getChannelType());
        /*调用序号*/
        String strIndex = xmlTreeUtil.getXmlTreeStringValue("currentDstCallIndex");
        Integer acctCallIndex = new Integer((StringUtil.isEmptyOrNull(strIndex)) ? "0" : strIndex);
        String dstAcctCallIndex = Integer.toString(acctCallIndex + 1);
        payOrderInfo.setDstCallIndex(acctCallIndex.toString());
        xmlTreeUtil.setXmlTreeStringValue("currentDstCallIndex", dstAcctCallIndex);
        payOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
        payOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        payOrderInfo.setChannelOrderId(GlobalEsbUtil.generateEsbSeqeuenceNumber(instId));
        payOrderInfo.setChannelReconKey(payOrderInfo.getChannelOrderId());
        payOrderInfo.setDstTxnNum("00260010999807");
        payOrderInfo.setPayType(PayTypeEnum.STAGE.getPayType());
        payOrderInfo.setPayerAcctInfo(installmentCancelRequest.getPayAcctNo());
        payOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        payOrderInfo.setPayerAcctInfo(origPayOrderInfo.getPayerAcctInfo());
        payOrderInfo.setPayerAcctType(AcctTypeEnum.CREDIT.getAcctType());
        payOrderInfo.setRealPayAmt(origPayOrderInfo.getRealPayAmt());
        payOrderInfo.setPayeeAcctInfo(origPayOrderInfo.getPayeeAcctInfo());
        payOrderInfo.setPayeeAcctType(origPayOrderInfo.getPayeeAcctType());
        payOrderInfo.setOrigSysSeqNum(origPayOrderInfo.getSysSeqNum());
        payOrderInfo.setOrigTxnDate(origPayOrderInfo.getTxnDate());
        payOrderInfo.setChannelBizType("0000");
        return payOrderInfo;
    }

    /**
     * 登记用户订单表
     *
     * @param installmentCancelRequest 请求信息
     * @throws Exception 异常
     */
    private UserOrderInfo initOrderInfoUser(InstallmentCancelRequest installmentCancelRequest, UserOrderInfo origUserOrderInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysNo = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        /*获取机构号*/
        String instId = installmentCancelRequest.getHeader().getInstId();
        /*获取交易码*/
        String txnNum = installmentCancelRequest.getHeader().getTxnNum();
        /*获取业务细分*/
        String bizType = installmentCancelRequest.getHeader().getBizType();
        /*获取源系统标识*/
        String msgSrcId = installmentCancelRequest.getHeader().getSrcSysId();
        userOrderInfo.setInstId(instId);
        userOrderInfo.setTxnDate(DateUtil.getCurrentDate());
        userOrderInfo.setTxnTime(DateUtil.getCurrentTime());
        userOrderInfo.setTxnNum(txnNum);
        userOrderInfo.setBizType(bizType);
        userOrderInfo.setMsgSrcId(msgSrcId);
        userOrderInfo.setTransChannelId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_CHANNEL_ID));
        userOrderInfo.setMchntOrderId(sysNo);
        userOrderInfo.setFrontSeqNum(installmentCancelRequest.getHeader().getTraceNo());
        userOrderInfo.setSysSeqNum(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        userOrderInfo.setOrderCreateTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"));
        userOrderInfo.setOrderExpiryTime(DateUtil.getDateByTimeStamp(System.currentTimeMillis() + 5 * 60 * 1000, "yyyyMMddHHmmss"));
        userOrderInfo.setOrderStatus(OrderStatusEnum.INIT.getStatus());
        userOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
        userOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        userOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        userOrderInfo.setOrderAmt(origUserOrderInfo.getOrderAmt());
        userOrderInfo.setOrigSysSeqNum(origUserOrderInfo.getSysSeqNum());
        userOrderInfo.setOrigTxnDate(origUserOrderInfo.getTxnDate());
        userOrderInfo.setUserId(installmentCancelRequest.getPayAcctNo());
        return userOrderInfo;
    }

    /**
     * 登记商户订单表
     *
     * @param installmentCancelRequest 请求信息
     * @throws Exception 异常
     */
    private MchntOrderInfo initOrderInfoMchnt(InstallmentCancelRequest installmentCancelRequest, UserOrderInfo userOrderInfo, MchntOrderInfo oriMchntOrderInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        /*获取商户基本信息*/
        MchntService mchntService = new MchntServiceImpl();
        MchntInfo mchntInfo = mchntService.getMchntInfo(installmentCancelRequest.getHeader().getInstId(), installmentCancelRequest.getMchntNo());
        if (null == mchntInfo) {
            rglog.error("查询商户基本信息失败<{}>", installmentCancelRequest.getMchntNo());
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        /*内部树复制会把订单状态保存到商户订单信息表*/
        BeanUtil.beanCopy(userOrderInfo, mchntOrderInfo);
        /*设置操作员id*/
        mchntOrderInfo.setUserId(null);
        /*设置商户订单表中业务产品代码*/
        mchntOrderInfo.setBizProdCode(txnInfo.getSrcTxnAuthCfg().getBizProdCode());
        /*收单机构号   从系统参数表中获取value值 即为收单机构号*/
        mchntOrderInfo.setAcqInstCode(mchntInfo.getSignInstId());
        /*行业类别*/
        mchntOrderInfo.setIndustryType(mchntInfo.getIndustryType());
        /*商户号*/
        mchntOrderInfo.setMchntNo(installmentCancelRequest.getMchntNo());
        /*若存在 设置连锁商户门店*/
        if (null != mchntInfo.getChainMchntNo()) {
            mchntOrderInfo.setChainMchntNo(mchntInfo.getChainMchntNo());
        }
        /*商户名称*/
        mchntOrderInfo.setMchntName(mchntInfo.getMchntName());
        /*交易币种*/
        mchntOrderInfo.setOrderCcy(AppParamConstant.DEFAULT_ORDER_CCY);
        /*交易金额*/
        mchntOrderInfo.setRealSettleAmt(mchntOrderInfo.getOrderAmt());
        /*实时清算标识*/
        mchntOrderInfo.setRealSettleFlag(mchntInfo.getRealSettleFlag());
        /*创建时间*/
        mchntOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        /*初始化订单状态*/
        mchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
        mchntOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        /*设置分期信息*/
        mchntOrderInfo.setProdStageCode(oriMchntOrderInfo.getProdStageCode());
        mchntOrderInfo.setStageNum(oriMchntOrderInfo.getStageNum());
        /*设置终端号*/
        mchntOrderInfo.setTermNo(installmentCancelRequest.getTermNo());
        return mchntOrderInfo;
    }

    /*银数 分期消费撤销*/
    private void updcCancel(InstallmentCancelRequest installmentCancelRequest, UserOrderInfo origUserOrderInfo,
                            MchntOrderInfo origMchntOrderInfo, PayOrderInfo origPayOrderInfoEsb, String newPin,
                            MchntOrderInfo mchntOrderInfo, UserOrderInfo userOrderInfo, PayOrderInfo payOrderInfoCancelESB,
                            String feeInputModeFlag, InstallmentCancelResponse installmentCancelResponse) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = installmentCancelRequest.getHeader().getInstId();
        OrderService orderService = new OrderServiceImpl();
        DubboService dubboService = new DubboServiceImpl();

        PosParamDealService posParamDealService = new PosParamDealServiceImpl();
        /*获取商户基本信息*/
        /*准备请求报文 调起esb接口*/
        // TODO 调用分期消费撤销,由项目组实现
        // (1) 通讯级失败, 更新订单为失败, 非特殊费率商户发起虚拟账户预授权撤销交易acctDealService.acctAuthCancel,
        // (2) 交易超时, 更新订单为超时.
        // (3) 交易成功,更新原三张订单表状态为撤销,更新撤销交易支付订单交易成功,
        //      非特殊费率商户发起虚拟账户预授权完成交易acctDealService.acctAuthCompPayment,
        //      调用营销冲正 comboDealService.cancelComboPayment,
        //      更新商户限额、终端限额、交易限额及笔数 mchntService.addTxnLimitStatis

        /* TODO 调用服务 */
        Result<ConsumeResponse> result = new Result<>();
        ConsumeResponse consumeResponse = new ConsumeResponse();
        consumeResponse.setRespCode(McpsParamConstant.DUBBO_SUCCESS);
        consumeResponse.setRespDesc("交易成功");
        consumeResponse.setSettleDate(DateUtil.getCurrentDate());
        result.setResult(consumeResponse);
        result.setSuccess(true);
        rglog.debug("分期消费：{}", JSON.toJSONString(result));
        /*撤销失败处理*/
        if (null == result || null == result.getResult()
                || (!McpsParamConstant.DUBBO_SUCCESS.equals(result.getResult().getRespCode())
                && !McpsParamConstant.DUBBO_OVERTIME.equals(result.getResult().getRespCode())
                && !McpsParamConstant.COMMUNICATION_OVERTIME.equals(result.getResult().getRespCode()))) {
            payOrderInfoCancelESB.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
            payOrderInfoCancelESB.setBizStatus(BizStatusEnum.FAILED.getStatus());
            mchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
            mchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
            userOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
            userOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
            if (!orderService.updateOrderInfo(userOrderInfo, Arrays.asList(mchntOrderInfo), payOrderInfoCancelESB)) {
                rglog.error("订单表更新失败");
                throw new BizException(RespCodeEnum.UPDATE_ORDER_ERROR.getRespCode(), RespCodeEnum.UPDATE_ORDER_ERROR.getRespDesc());
            }
            /*设置返回码 返回信息*/
            if (null == result || !result.isSuccess()) {
                payOrderInfoCancelESB.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                payOrderInfoCancelESB.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            } else {
                payOrderInfoCancelESB.setRespCode(result.getResult().getRespCode());
                payOrderInfoCancelESB.setRespDesc(result.getResult().getRespDesc());
            }
        }
        //撤销超时处理
        /*判断是否超时*/
        else if (result.isSuccess()
                && (McpsParamConstant.DUBBO_OVERTIME.equals(result.getResult().getRespCode())
                || McpsParamConstant.COMMUNICATION_OVERTIME.equals(result.getResult().getRespCode()))) {
            //更新撤销订单状态为失败,并更新原交易系统流水号、交易日期
            payOrderInfoCancelESB.setRespCode(result.getResult().getRespCode());
            payOrderInfoCancelESB.setRespDesc(result.getResult().getRespDesc());
            payOrderInfoCancelESB.setTransStatus(TransStatusEnum.TRANS_TIMEOUT.getStatus());
            //更新商户订单状态为失败，并更新原交易系统流水号、交易日期
            mchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_TIMEOUT.getStatus());
            List<MchntOrderInfo> listMchntOrderInfo = new ArrayList<>();
            listMchntOrderInfo.add(mchntOrderInfo);
            //更新用户订单状态为失败，并更新原交易系统流水号、交易日期
            userOrderInfo.setTransStatus(TransStatusEnum.TRANS_TIMEOUT.getStatus());
            if (!orderService.updateOrderInfo(userOrderInfo, listMchntOrderInfo, payOrderInfoCancelESB)) {
                rglog.error("订单表更新失败");
                throw new BizException(RespCodeEnum.UPDATE_ORDER_ERROR.getRespCode(), RespCodeEnum.UPDATE_ORDER_ERROR.getRespDesc());
            }
        }
        //撤销成功处理
        else {
            /*更新原三张订单表状态 为 已撤销*/
            origMchntOrderInfo.setBizStatus(BizStatusEnum.CANCEL.getStatus());
            origPayOrderInfoEsb.setBizStatus(BizStatusEnum.CANCEL.getStatus());
            origUserOrderInfo.setBizStatus(BizStatusEnum.CANCEL.getStatus());
            List<MchntOrderInfo> origListMchntOrderInfo = new ArrayList<>();
            origListMchntOrderInfo.add(origMchntOrderInfo);
            orderService.updateOrderInfo(origUserOrderInfo, origListMchntOrderInfo, origPayOrderInfoEsb);
            //更新撤销交易支付订单交易成功
            payOrderInfoCancelESB.setRespCode(result.getResult().getRespCode());
            payOrderInfoCancelESB.setRespDesc(result.getResult().getRespDesc());
            payOrderInfoCancelESB.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
            payOrderInfoCancelESB.setBizStatus(BizStatusEnum.FINISH.getStatus());
            payOrderInfoCancelESB.setSettleDate(result.getResult().getSettleDate());
            //更新撤销交易商户订单交易成功
            mchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
            mchntOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
            List<MchntOrderInfo> listMchntOrderInfo = new ArrayList<>();
            listMchntOrderInfo.add(mchntOrderInfo);
            //更新撤销交易用户订单交易成功
            userOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
            userOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
            userOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
            if (!orderService.updateOrderInfo(userOrderInfo, listMchntOrderInfo, payOrderInfoCancelESB)) {
                rglog.error("订单表更新失败");
                throw new BizException(RespCodeEnum.UPDATE_ORDER_ERROR.getRespCode(), RespCodeEnum.UPDATE_ORDER_ERROR.getRespDesc());
            }

            //更新商户限额、终端限额、交易限额及笔数
            MchntService mchntService = new MchntServiceImpl();
            mchntService.updateMchntTransLimit(mchntOrderInfo);
        }

        /*设置返回码 返回信息*/
        if (null == result || !result.isSuccess()) {
            installmentCancelResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
            installmentCancelResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
        } else {
            installmentCancelResponse.setRespCode(result.getResult().getRespCode());
            installmentCancelResponse.setRespDesc(result.getResult().getRespDesc());
            installmentCancelResponse.setPayId(payOrderInfoCancelESB.getSysSeqNum());
            installmentCancelResponse.setSettleDate(result.getResult().getSettleDate());
        }
    }
}
